<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ include file="/WEB-INF/page/include/taglib.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>文章分类</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT">
<meta name="format-detection" content="telephone=no">
<link rel="stylesheet" href="${ctx }/layui/css/layui.css" media="all" />
<link rel="stylesheet" href="${ctx }/css/font_eolqem241z66flxr.css"
	media="all" />
<link rel="stylesheet" href="${ctx }/css/list.css" media="all" />
<script>
	var ctx = "${ctx}";
</script>
</head>
<body class="childrenBody">
	<input type="hidden" id="categoryId"
		value="<shiro:principal property="id"/>" />
	<blockquote class="layui-elem-quote list_search">
		<shiro:hasPermission name="category:category:save">
			<div class="layui-inline">
				<a class="layui-btn layui-btn-normal categoryAdd_btn"><i
					class="layui-icon">&#xe608;</i> 添加分类</a>
			</div>
		</shiro:hasPermission>
		<shiro:hasPermission name="category:category:delete">
			<div class="layui-inline">
				<a class="layui-btn layui-btn-danger batchDel"><i
					class="layui-icon">&#xe640;</i>批量删除</a>
			</div>
		</shiro:hasPermission>
		<!-- <div class="layui-inline">
			<div class="layui-form-mid layui-word-aux"></div>
		</div> -->
	</blockquote>
	<table id="categoryList" lay-filter="categoryList"></table>
	<script type="text/javascript" src="${ctx }/layui/layui.js"></script>
	<script type="text/html" id="barEdit">
  		<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  		<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	</script>
	<script>
	layui.config({
		base : "js/"
	}).use(['form','layer','jquery','laypage','table','laytpl'],function(){
		var form = layui.form,table = layui.table;
			layer = parent.layer === undefined ? layui.layer : parent.layer,
			laypage = layui.laypage,
			$ = layui.jquery;
			//数据表格
			table.render({
				id:'categoryList',
			    elem: '#categoryList'
			    ,url: ctx+'/category/getCategoryList' //数据接口
			    ,cellMinWidth: 80
			    ,limit:10//每页默认数
			    ,limits:[10,20,30,40]
			    ,cols: [[ //表头
	              {type:'checkbox'}
	              ,{field:'id', title: 'ID', sort: true}
	              ,{field:'category', title: '分类名'}
	              ,{title: '操作',toolbar: '#barEdit'}
			    ]]
					,page: true //开启分页
					,where: {timestamp: (new Date()).valueOf()}
			  });
			//根据id删除分类
			table.on("tool(categoryList)",function(obj){
				var  data = obj.data;
				if(obj.event === "del"){
					layer.confirm('真的删除行么', function(index) {
						$.ajax({
							url : ctx + '/category/delCategoryByid',
							data:{id:data.id},									
							type : "get",
							success : function(d) {
								if (d.code == 0) {
									obj.del();
								} else {
									layer.msg(data.msg, {
										icon : 5
									});
								}
							}
						})
						layer.close(index);
					});
				}else if(obj.event === 'edit'){
					var index = layui.layer.open({
						title : "添加分类",
						type : 2,
						content : ctx+"/category/editCategory/"+data.id ,
						success : function(layero, index){
							
						}
					})
					/* layer.open({
				    	  type: 2,
				    	  title:"编辑分类",
				    	  area: ['380px', '560px'],
				    	  content:ctx+"/category/editCategory/"+data.id //这里content是一个普通的String
				      }) */
				}
			});
			$(".batchDel").on('click',function(){
				var checkStatus = table.checkStatus('categoryList'),
				data = checkStatus.data,
				categoryStr = '';
				console.log(data.length);
				if(data.length>0){
					$.each(data, function(n, value) {
						categoryStr += value.id + ',';
					});
					categoryStr = categoryStr.substring(0, categoryStr.length - 1);
					layer.confirm('确定删除<strong>' + data.length + '</strong>条数据吗？', function(index) {
						$.ajax({
							url: ctx + '/category/delCategory', //接口地址
							type: "get",
							data:{categoryStr:categoryStr},
							success: function(d) {
								if(d.code == 0) {
									//删除成功，刷新父页面
									parent.location.reload();
								} else {
									layer.msg("权限不足，联系超管！", {
										icon: 5
									});
								}
							}
						})
					});
				}else {
					layer.msg("请选择需要删除的分类");
				} 
			});
			
			//添加分类
			$(".categoryAdd_btn").click(function(){
			var index = layui.layer.open({
			title : "添加分类",
			type : 2,
			content : ctx+"/category/addCategory",
			success : function(layero, index){
				
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})
	})
	</script>
</body>
</html>